//
//  WZTabBarController.m
//  ETong
//
//  Created by wanzhao on 15/10/28.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZTabBarController.h"
#import "WZPersonalController.h"
#import "WZOfficeController.h"
#import "WZNavigationController.h"
#import "EfengTabView.h"


@interface WZTabBarController ()<EfengTabViewDelegate, UITabBarControllerDelegate>

@property (nonatomic, strong) EfengTabView *tabBarView;
@property (nonatomic, weak) WZOfficeController *office;
@property (nonatomic, weak) WZPersonalController *person;

@end

@implementation WZTabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
    
}


- (void)dealloc
{
    NSLog(@"WZTabBarController dealloc");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.view.backgroundColor = RedColor;
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.delegate = self;
    self.title = @"办公中心";

    WZOfficeController *discover = [[WZOfficeController alloc] init];
    
    [self addOneController:discover title:@"办公中心" imageName:@"office_center_normal" selectedImageName:@"office_center_pressed" tag:0];
    self.office = discover;
    
    WZPersonalController *order = [[WZPersonalController alloc] init];
    [self addOneController:order title:@"个人中心" imageName:@"personal_center_normal" selectedImageName:[NSString stringWithFormat:@"personal_center_pressed"] tag:1];
    self.person = order;
    
}

- (EfengTabView *)tabBarView
{
    if (_tabBarView == nil) {
        _tabBarView = [[EfengTabView alloc] init];
        _tabBarView.backgroundColor = WhiteColor;
        _tabBarView.eFengTabDelegate = self;
        _tabBarView.frame = self.tabBar.bounds;
        [self.tabBar addSubview:_tabBarView];
    }
    return _tabBarView;
}

//必须在以下两个方法中都要删除系统的tabar，否则自定义的tabbar会有黑线条或者重影
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    for (UIView *subView in self.tabBar.subviews) {
        if ([subView isKindOfClass:[UIControl class]]) {
            [subView removeFromSuperview];
        }
    }
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    for (UIView *subView in self.tabBar.subviews) {
        
        if ([subView isKindOfClass:[UIControl class]]) {
            [subView removeFromSuperview];
        }
    }
}

- (void)addOneController:(EFengAndETongTabBaseController *)viewCotroller title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName tag:(NSInteger)tag
{
    EFengAndETongTabBarItem *barItem = [[EFengAndETongTabBarItem alloc] init];
    barItem.selectedImageName = selectedImageName;
    barItem.normalImageName = imageName;
    barItem.badgeValue = @"0";
    barItem.redDianValue = @"0";
    
    viewCotroller.badgeItem = barItem;
    WZNavigationController *nav = [[WZNavigationController alloc] initWithRootViewController:viewCotroller];
    [self addChildViewController:nav];
    
    
    [self.tabBarView eFengTableViewAddButtonWithItem:barItem];
    
}

- (void)eFengTabBarViewSelectControllerFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toindex
{
    self.selectedIndex = toindex;
    if (toindex == 0) {
        self.title = @"办公中心";
    }else if (toindex == 1){
        self.title = @"个人中心";
    }else{
        
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end